<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Manage_product extends CI_Controller{

		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_product");
			$this->load->model("apanel/db_news");
			$this->load->library('pagination');
		}
		
		public function index(){
			//== template
			redirect('manage_product/cate_list');
		}
		
		//=== category
		public function cate_list($st = ""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if(is_numeric($st)){
				$subcate=$st;
			}else{
				$subcate="";
			}
			list($cat,$num,$page,$total) = $this->db_product->cate_all($subcate);
			$this->cismarty->assign("Num_Rows",$num);
			$this->cismarty->assign("cat",$cat);
			$this->cismarty->assign("pagelink",$page);
			$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("navi",$this->navi($st));
			$this->cismarty->assign("id",$st);
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_product/cat_dellAll'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/cate-list.html');
		}
		
		private function navi($id){
			$str = "";
			if($id != 0){
				$this->db->where("pkid",$id);
				$row = $this->db->get("mobile_product_category")->row();
				$str .= " -> ".$row->name_en.'  ';
				//echo "parnet = ",$row->parent_pkid;
					//subcate
					$this->db->where("pkid",$row->parent_pkid);
					$num = $this->db->count_all_results("mobile_product_category");
					if($num > 0){
						$this->db->where("pkid",$row->parent_pkid);
						$row2 = $this->db->get("mobile_product_category")->row();
						$str ="  -> ".$row2->name_en.$str;
						
					}
			}
			return $str;
		}
		
	
		
	public function cat_add($subid=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("title_en","Category name","required");
				$this->form_validation->set_rules("title_cn","Category name","trim");
				$this->form_validation->set_rules("title_th","Category name","trim");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("desc_en","Description","required");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("customer",set_value('customer'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
						
					//set error
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
					$this->cismarty->assign("errorcustomer",form_error('customer'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/product/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 100;
							$config['height'] = 70;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}else{
						$picname="";
					}
					if(!$error){
						if($subid == "")
							$subid = 0;
						
						$data = array(
								"customer_pkid"=>trim($this->input->post('customer')),
								"parent_pkid"=>$subid,
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_cn"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>trim($this->input->post('desc_en')),
								"description_zh_cn"=>trim($this->input->post('desc_cn')),
								"description_th"=>trim($this->input->post('desc_th')),
								"img"=>$picname,
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$res = $this->db->insert('mobile_product_category',$data);
						if($res) redirect("manage_product/cate_list/$subid");
						else  redirect('manage_product/cate_list/fail');
					}
				}
			}
			$this->cismarty->assign("customer",$this->db_news->customer_list());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/cat_add/$subid"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->assign("navi",$this->navi($subid));
			$this->cismarty->assign("url",base_url());
			$this->cismarty->display('apanel/product/cate_add.html');
		}
		
		public function cat_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("title_en","Category name","required");
				$this->form_validation->set_rules("title_cn","Category name","trim");
				$this->form_validation->set_rules("title_th","Category name","trim");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("desc_en","Description","required");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
			
					//set error
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
					$this->cismarty->assign("errorcustomer",form_error('customer'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/product/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 100;
							$config['height'] = 70;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$this->db->where("pkid",$id);
							$uppic = array("img"=>$picname);
							$this->db->update("mobile_product_category",$uppic);
								
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"customer_pkid"=>$this->input->post('customer'),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_cn"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>trim($this->input->post('desc_en')),
								"description_zh_cn"=>trim($this->input->post('desc_cn')),
								"description_th"=>trim($this->input->post('desc_th')),
								"updated_by"=>$this->session->userdata("sess_username"),
								"updated_date"=>date("Y-m-d H:i:s")
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('mobile_product_category',$data);
						if($res) redirect('manage_product/cate_list/success');
						else  redirect('manage_product/cate_list/fail');
					}
				}
			}else {
				$this->db->where("pkid",$id);
				$res = $this->db->get('mobile_product_category');
				$row = $res->row();
				$this->cismarty->assign("title_en",$row->name_en);
				$this->cismarty->assign("title_cn",$row->name_zh_cn);
				$this->cismarty->assign("title_th",$row->name_th);
				$this->cismarty->assign("customer",$this->db_news->customer_list($row->customer_pkid));
				$this->cismarty->assign("desc_en",stripslashes($row->description_en));
				$this->cismarty->assign("desc_cn",stripslashes($row->description_zh_cn));
				$this->cismarty->assign("desc_th",stripslashes($row->description_th));
				if(trim($row->img) != "") $this->cismarty->assign("img",'<img src="'.base_url().'media/product/'.$row->img.'" width="228" height="170"><br>');
 			}
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/cat_edit/$id"));
			$this->cismarty->display("apanel/product/cate_edit.html");
			
		}
		
		public function cat_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_product_category");
				if($res) redirect('manage_product/cate_list/success');
			}
		}
		
		public function cat_dellAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_product_category");
				}
			}
			redirect('manage_product/cate_list/success');
		}
		
		//===== product 
		
		public function product_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			//list($item,$num,$page,$total) = $this->listall();
			//$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
		
			
			$scripts = '
			<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
			$(\'#table1\').dataTable( {
			"bJQueryUI": true,
			"bProcessing": true,
			"bServerSide": true,
			"sAjaxSource": "'.base_url().'manage_product/product_item_list",
			"iDisplayStart"  : 0,
			"sPaginationType": "full_numbers",
			"aaSorting": [[ 0, "desc" ]],
			"aoColumnDefs": [
			{ "bSortable": false, "aTargets": [ 0 ] },
			{ "bSortable": false, "aTargets": [ 3 ] },
			{ "bSortable": false, "aTargets": [ 4 ] },
			],
				"fnServerData": function(sSource, aoData, fnCallback)
			{
			$.ajax(
			{
			\'dataType\': \'json\',
			\'type\'  : \'POST\',
			\'url\'    : sSource,
			\'data\'  : aoData,
			\'success\' : fnCallback
			}
			);
			}
			} );
			} );
			</script>';
			
			if($this->session->userdata('sess_customer_name') =='huawei' || $this->session->userdata('sess_access')== 'superadmin'){
			
				$excel=" <ul id='icon'>
        	<li><a href='".base_url()."manage_excel/excel_add'><img src='".base_url()."images/serial.png' /></a></li>
        	<li><a href='".base_url()."manage_excel/excel_add'><img src='".base_url()."images/excel.png' /></a></li>
           </ul>";
		   }else{
		   		$excel="";
		   }
			
			
			//== template
			$this->cismarty->assign("excel",$excel);
			$this->cismarty->assign("scripts",$scripts);
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_product/product_delAll"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/product_list.html');
		}
		
		
			public function product_item_list(){
			$output = $this->db_product->product_item_list();
			echo json_encode($output);
		}
		
		
		
			
		
		public function product_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("customer","Customer","trim|required");
				$this->form_validation->set_rules("category","Category","trim|required");
				$this->form_validation->set_rules("code","code","trim|required");
				$this->form_validation->set_rules("title_en","name","required");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
				$this->form_validation->set_rules("desc_en","Description","required");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				$this->form_validation->set_rules("price","Price","trim|required|numeric");
				$this->form_validation->set_rules("point_gain","Point gain","trim|required|numeric");
				$this->form_validation->set_rules("point_spend","Point spend","trim|required|numeric");
			
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
					$this->cismarty->assign("price",set_value('price'));
					$this->cismarty->assign("point_gain",set_value('point_gain'));
					$this->cismarty->assign("point_spend",set_value('point_spend'));
					$this->cismarty->assign("code",set_value('code'));
			
					//set error
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
					$this->cismarty->assign("errorcategory",form_error('category'));
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					$this->cismarty->assign("errorstatus",form_error('status'));
					$this->cismarty->assign("errorprice",form_error('price'));
					$this->cismarty->assign("errorpoint_gain",form_error('point_gain'));
					$this->cismarty->assign("errorpoint_spend",form_error('point_spend'));
					$this->cismarty->assign("errorcode",form_error('code'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/product/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 256;
							$config['height'] = 220;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}else{
						$picname="";
					}
					if(!$error){
						$data = array(
								"customer_pkid"=>$this->input->post('customer'),
								"category_pkid"=>$this->input->post('category'),
								"code"=>$this->input->post('code'),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_cn"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>addslashes(trim($this->input->post('desc_en'))),
								"description_zh_cn"=>addslashes(trim($this->input->post('desc_cn'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"price"=>$this->input->post('price'),
								"point_gain"=>$this->input->post('point_gain'),
								"point_spend"=>$this->input->post('point_spend'),
								"img"=>$picname,
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$res = $this->db->insert('mobile_product_item',$data);
						if($res) redirect('manage_product/product_list/success');
						else  redirect('manage_product/product_list/fail');
					}
				}
			}//end post
				
				
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("customer",$this->db_product->show_customer());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/product_add"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/product_add.html');
		}
		
		public function cate_no($id,$group=""){
			//echo "group=".$group;
			if($id == "") $id=0;
			$this->db->where('customer_pkid',$id);
			$this->db->where("parent_pkid",0);
			$this->db->order_by("pkid","asc");
			$res = $this->db->get('mobile_product_category');
			$str ='<select name="category">';
			$str .= '<option value="">=select Category=</option>';
			foreach ($res->result_array() as $P){
				if($group == $P['pkid']){
					$check = 'selected="selected"';
				}else {
					$check = "";
				}
				$str .='<option value="'.$P['pkid'].'" '.$check.'>'.$P['name_en'].'</option>';
				$this->db->where("parent_pkid",$P['pkid']);
				$num = $this->db->count_all_results("mobile_product_category");
				if($num > 0){
					$this->db->where("parent_pkid",$P['pkid']);
					$res2 = $this->db->get("mobile_product_category")->result_array();
					foreach ($res2 as $sub){
						if($group == $sub['pkid']){
							$check = 'selected="selected"';
						}else {
							$check = "";
						}
						$str .='<option value="'.$sub['pkid'].'" '.$check.'>.....'.$sub['name_en'].'</option>';
						$this->db->where("parent_pkid",$sub['pkid']);
						$num2 = $this->db->count_all_results("mobile_product_category");
						if($num2 > 0){
							$this->db->where("parent_pkid",$sub['pkid']);
							$res3 = $this->db->get("mobile_product_category")->result_array();
							foreach ($res3 as $sub2){
								if($group == $sub2['pkid']){
									$check = 'selected="selected"';
								}else {
									$check = "";
								}
								$str .='<option value="'.$sub2['pkid'].'" '.$check.'>................'.$sub2['name_en'].'</option>';
							}
						}
					}
				}
			}
			$str .="</select>";
			echo $str;
		}
		
		public function product_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("category","Category","trim|required");
				$this->form_validation->set_rules("code","code","trim|required");
				$this->form_validation->set_rules("title_en","name","required");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
				$this->form_validation->set_rules("desc_en","Description","required");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				$this->form_validation->set_rules("price","Price","trim|required|numeric");
				$this->form_validation->set_rules("point_gain","Point gain","trim|required|numeric");
				$this->form_validation->set_rules("point_spend","Point spend","trim|required|numeric");
			
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
					$this->cismarty->assign("price",set_value('price'));
					$this->cismarty->assign("point_gain",set_value('point_gain'));
					$this->cismarty->assign("point_spend",set_value('point_spend'));
					$this->cismarty->assign("code",set_value('code'));
			
					//set error
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
					$this->cismarty->assign("errorcategory",form_error('category'));
					$this->cismarty->assign("errorstatus",form_error('status'));
					$this->cismarty->assign("errorprice",form_error('price'));
					$this->cismarty->assign("errorpoint_gain",form_error('point_gain'));
					$this->cismarty->assign("errorpoint_spend",form_error('point_spend'));
					$this->cismarty->assign("errorcode",form_error('code'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/product/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 256;
							$config['height'] = 220;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$uppic = array("img"=>$picname);
							$this->db->where("pkid",$id);
							$this->db->update('mobile_product_item',$uppic);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"category_pkid"=>$this->input->post('category'),
								"code"=>$this->input->post('code'),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_cn"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>addslashes(trim($this->input->post('desc_en'))),
								"description_zh_cn"=>addslashes(trim($this->input->post('desc_cn'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"price"=>$this->input->post('price'),
								"point_gain"=>$this->input->post('point_gain'),
								"point_spend"=>$this->input->post('point_spend'),
								"status"=>$this->input->post('status'),
								"updated_by"=>$this->session->userdata("sess_username"),
								"updated_date"=>date("Y-m-d H:i:s")
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('mobile_product_item',$data);
						if($res) redirect('manage_product/product_list/success');
						else  redirect('manage_product/product_list/fail');
					}
				}
			}else{
				$this->db->where("pkid",$id);
				$res = $this->db->get("mobile_product_item");
				$row = $res->row();
				$this->cismarty->assign("title_en",$row->name_en);
				$this->cismarty->assign("title_cn",$row->name_zh_cn);
				$this->cismarty->assign("title_th",$row->name_th);
				$this->cismarty->assign("desc_en",stripslashes($row->description_en));
				$this->cismarty->assign("desc_cn",stripslashes($row->description_zh_cn));
				$this->cismarty->assign("desc_th",stripslashes($row->description_th));
				$this->cismarty->assign("price",$row->price);
				$this->cismarty->assign("point_gain",$row->point_gain);
				$this->cismarty->assign("point_spend",$row->point_spend);
				$this->cismarty->assign("code",$row->code);
				$group 	= $row->customer_pkid;
				$cate		= $row->category_pkid;
				if(trim($row->img) != ""){
					$this->cismarty->assign("img",'<img src="'.base_url().'media/product/'.$row->img.'"><br>');
				}
				if($row->status == 1) $this->cismarty->assign("sa",'checked="checked"');
				else $this->cismarty->assign("sd",'checked="checked"');
			}
				
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("customer",$this->db_product->show_customer($group));
			$this->cismarty->assign("cid","".$group."/$cate");
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/product_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/product_edit.html');
		}
		
		public function product_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_product_item");
				if($res) redirect('manage_product/product_list/success');
			}
		}
		
		public function product_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_product_item");
				}
			}
			redirect('manage_product/product_list/success');
		}
		
		//===== picture
		
		public function picture_list($id){
			$arr = array();
			$this->db->where("product_pkid",$id);
			$this->db->order_by("pkid","desc");
			$res =$this->db->get("mobile_product_pic")->result_array();
			foreach ($res as $P){
				$tmp['pkid']		=$P['pkid'];
				$tmp['name']		=$P['name'];
				$tmp['img']		='<img src="'.base_url().'media/pro_pic/'.$P['img'].'">';
				$tmp['cid']			=$id;
				$arr[] =$tmp;
			}
		
			$this->cismarty->assign("picture",$arr);
		
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("id",$id);
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/product_img_del_all/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/picture_list.html');
		}
		
		public function product_img_add($cid){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("name","name","required");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					
					$this->cismarty->assign("errortitle_en",form_error('name'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/pro_pic/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						//$config['create_thumb'] = TRUE;
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							//$picnamethumb = date('YmdHis').'_thumb'.$data['file_ext'];
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 640;
							$config['height'] = 390;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"product_pkid"=>$cid,
								"name"=>trim($this->input->post('name')),
								"img"=>$picname,
						);
						$res = $this->db->insert('mobile_product_pic',$data);
						if($res){
							if($cid != ""){
								redirect("manage_product/picture_list/$cid");
							}else{
								redirect('manage_product/product_list/success');
							}
						}
						else  redirect('manage_product/product_list/fail');
					}
				}
			}//end post
				
			$this->cismarty->assign("customer",$this->db_news->customer_list($cid));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("cid",$cid);
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/product_img_add/$cid"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/product_img_add.html');
		}
		
		
		
		public function product_img_edit($id,$cid=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("name","Name","required");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("errortitle_en",form_error('name'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/pro_pic/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$config['create_thumb'] = TRUE;
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 640;
							$config['height'] = 390;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$uppic = array("img"=>$picname);
							$this->db->where("pkid",$id);
							$this->db->update('mobile_product_pic',$uppic);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"name"=>trim($this->input->post('name'))
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('mobile_product_pic',$data);
						if($res){
							if($cid != ""){
								redirect("manage_product/picture_list/$cid");
							}else{
								redirect('manage_product/product_list/success');
							}
						}
						else  redirect('manage_product/product_list/fail');
					}
				}
			}//end post
			else{
				$this->db->where("pkid",$id);
				$row = $this->db->get("mobile_product_pic")->row();
				$this->cismarty->assign("name",$row->name);
				if(trim($row->img) != ""){
					$this->cismarty->assign("img",'<img src="'.base_url().'media/pro_pic/'.$row->img.'"><br>');
				}
			}
		
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("cid",$cid);
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/product_img_edit/$id/$cid"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/product_img_edit.html');
		}
		
		
		
		public function product_img_del($id,$cid){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_product_pic");
				if($res) redirect("manage_product/picture_list/$cid");
			}
		}
		
		public function product_img_del_all($id){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_product_pic");
				}
			}
			redirect("manage_product/picture_list/$id");
		}
		
		//==Promotion
		
		public function promotion_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($item,$num,$page,$total) = $this->db_product->promotion_all();
			$this->cismarty->assign("Num_Rows",$num);
			$this->cismarty->assign("item",$item);
			$this->cismarty->assign("pagelink",$page);
			$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,3));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_product/promotion_delAll"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/promotion_list.html');
		}
		
		public function get_product(){
			$q = $_REQUEST['q'];	
			$recordset = $this->db->get('hotel_product_item');
			foreach ($recordset->result_array() as $arrtmp){
				$items[$arrtmp['pkid']] = $arrtmp['name_en'];
			}
				
			foreach ($items as $key=>$value) {
					if (strpos(strtolower($value), $q) !== false) {
						echo "$key|$value\n";
					}
			}
		}
		
		public function promotion_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("code","code","trim|required");
				$this->form_validation->set_rules("title_en","name","required");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
				$this->form_validation->set_rules("desc_en","Description","trim");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				$this->form_validation->set_rules("price","Price","trim|required|numeric");
				$this->form_validation->set_rules("point_gain","Point gain","trim|required|numeric");
				$this->form_validation->set_rules("point_spend","Point spend","trim|required|numeric");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
					$this->cismarty->assign("price",set_value('price'));
					$this->cismarty->assign("point_gain",set_value('point_gain'));
					$this->cismarty->assign("point_spend",set_value('point_spend'));
					$this->cismarty->assign("code",set_value('code'));
						
					//set error
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
					$this->cismarty->assign("errorgroup",form_error('group'));
					$this->cismarty->assign("errorstatus",form_error('status'));
					$this->cismarty->assign("errorprice",form_error('price'));
					$this->cismarty->assign("errorpoint_gain",form_error('point_gain'));
					$this->cismarty->assign("errorpoint_spend",form_error('point_spend'));
					$this->cismarty->assign("errorcode",form_error('code'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/promotion/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 150;
							$config['height'] = 150;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"product_pkid"=>$this->input->post('pkid'),
								"code"=>$this->input->post('code'),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_cn"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>addslashes(trim($this->input->post('desc_en'))),
								"description_zh_cn"=>addslashes(trim($this->input->post('desc_cn'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"price"=>$this->input->post('price'),
								"start_date"=>$this->input->post('startdate'),
								"end_date"=>$this->input->post('enddate'),
								"point_gain"=>$this->input->post('point_gain'),
								"point_spend"=>$this->input->post('point_spend'),
								"img"=>$picname,
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$res = $this->db->insert('hotel_promotion',$data);
						if($res) redirect('manage_product/promotion_list/success');
						else  redirect('manage_product/promotion_list/fail');
					}
				}
			}//end post
			
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("scripts",$this->db_product->get_script());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,3));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/promotion_add"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/promotion_add.html');
		}
		
		public function promotion_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("code","code","trim|required");
				$this->form_validation->set_rules("title_en","name","required");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
				$this->form_validation->set_rules("desc_en","Description","trim");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				$this->form_validation->set_rules("price","Price","trim|required|numeric");
				$this->form_validation->set_rules("point_gain","Point gain","trim|required|numeric");
				$this->form_validation->set_rules("point_spend","Point spend","trim|required|numeric");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
					$this->cismarty->assign("price",set_value('price'));
					$this->cismarty->assign("point_gain",set_value('point_gain'));
					$this->cismarty->assign("point_spend",set_value('point_spend'));
					$this->cismarty->assign("code",set_value('code'));
						
					//set error
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
					$this->cismarty->assign("errorgroup",form_error('group'));
					$this->cismarty->assign("errorstatus",form_error('status'));
					$this->cismarty->assign("errorprice",form_error('price'));
					$this->cismarty->assign("errorpoint_gain",form_error('point_gain'));
					$this->cismarty->assign("errorpoint_spend",form_error('point_spend'));
					$this->cismarty->assign("errorcode",form_error('code'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/promotion/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 250;
							$config['height'] = 250;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$uppic = array("img"=>$picname);
							$this->db->where("pkid",$id);
							$this->db->update('hotel_promotion',$uppic);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"product_pkid"=>$this->input->post('pkid'),
								"code"=>$this->input->post('code'),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_cn"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>addslashes(trim($this->input->post('desc_en'))),
								"description_zh_cn"=>addslashes(trim($this->input->post('desc_cn'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"price"=>$this->input->post('price'),
								"start_date"=>$this->input->post('startdate'),
								"end_date"=>$this->input->post('enddate'),
								"point_gain"=>$this->input->post('point_gain'),
								"point_spend"=>$this->input->post('point_spend'),
								"status"=>$this->input->post('status'),
								"updated_by"=>$this->session->userdata("sess_username"),
								"updated_date"=>date("Y-m-d H:i:s")
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('hotel_promotion',$data);
						if($res) redirect('manage_product/promotion_list/success');
						else  redirect('manage_product/promotion_list/fail');
					}
				}
			}else{
				$this->db->where("pkid",$id);
				$res = $this->db->get("hotel_promotion");
				$row = $res->row();
				$this->cismarty->assign("title_en",$row->name_en);
				$this->cismarty->assign("title_cn",$row->name_zh_cn);
				$this->cismarty->assign("title_th",$row->name_th);
				$this->cismarty->assign("desc_en",stripslashes($row->description_en));
				$this->cismarty->assign("desc_cn",stripslashes($row->description_zh_cn));
				$this->cismarty->assign("desc_th",stripslashes($row->description_th));
				$this->cismarty->assign("price",$row->price);
				$this->cismarty->assign("point_gain",$row->point_gain);
				$this->cismarty->assign("point_spend",$row->point_spend);
				$this->cismarty->assign("code",$row->code);
				$this->load->model("apanel/db_banner");
				$this->cismarty->assign("startdate",$this->db_banner->stripdate($row->start_date));
				$this->cismarty->assign("enddate",$this->db_banner->stripdate($row->end_date));
				$pro_pkid = $row->product_pkid;
				if(trim($row->img) != ""){
					$this->cismarty->assign("img",'<img src="'.base_url().'media/promotion/'.$row->img.'"><br>');
				}
				if($row->status == 1) $this->cismarty->assign("sa",'checked="checked"');
				else $this->cismarty->assign("sd",'checked="checked"');
			}
			
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("scripts",$this->db_product->get_script());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,3));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/promotion_edit/$id"));
			$this->cismarty->assign("pkid",$pro_pkid);
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/promotion_edit.html');
		}
		
		public function get_update_product($code){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');		
			$code = rawurldecode($code);
			if($code != ""){
			//	list($c,$n)  = explode(":", $code); 
				$this->db->where("name_en",$code);
				$this->db->limit(1);
				$res = $this->db->get('hotel_product_item');
				$row = $res->row();
				//print_r($row);
				$pkid[]			= $row->pkid;
				$codes[] 	= $row->code;
				$title_en[] = $row->name_en;
				$title_cn[]	= $row->name_zh_cn;
				$title_th[] 	= $row->name_th;
				$price[]		=$row->price;
				$point_gain = $row->point_gain;
				$point_spend=$row->point_spend;
				echo json_encode(compact('pkid','codes','title_en','title_cn','title_th','price','point_gain','point_spend'));
			}
		}
		
		public function promotion_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("hotel_promotion");
				if($res) redirect('manage_product/promotion_list/success');
			}
		}
		
		public function promotion_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("hotel_promotion");
				}
			}
			redirect('manage_product/promotion_list/success');
		}
		
		public function transport_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($item,$num,$page,$total) = $this->db_product->transport_all();
			$this->cismarty->assign("Num_Rows",$num);
			$this->cismarty->assign("item",$item);
			$this->cismarty->assign("pagelink",$page);
			$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(3,7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_product/transport_delAll"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/transport_list.html');
		}
		
		public function transport_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("journey","journey","trim");
				$this->form_validation->set_rules("distance","distance","trim");
				$this->form_validation->set_rules("title_en","name","required");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
				$this->form_validation->set_rules("desc_en","Description","required");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				$this->form_validation->set_rules("price","Price","trim|required|numeric");
				$this->form_validation->set_rules("point_gain","Point gain","trim|required|numeric");
				$this->form_validation->set_rules("point_spend","Point spend","trim|required|numeric");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
					$this->cismarty->assign("price",set_value('price'));
					$this->cismarty->assign("point_gain",set_value('point_gain'));
					$this->cismarty->assign("point_spend",set_value('point_spend'));
					$this->cismarty->assign("journey",set_value('journey'));
					$this->cismarty->assign("distance",set_value('distance'));
						
					//set error
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
					$this->cismarty->assign("errorstatus",form_error('status'));
					$this->cismarty->assign("errorprice",form_error('price'));
					$this->cismarty->assign("errorpoint_gain",form_error('point_gain'));
					$this->cismarty->assign("errorpoint_spend",form_error('point_spend'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/transport/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= '800';
 							$config['height'] = 'auto';
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"journey"=>$this->input->post('journey'),
								"distance"=>$this->input->post('distance'),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_cn"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>addslashes(trim($this->input->post('desc_en'))),
								"description_zh_cn"=>addslashes(trim($this->input->post('desc_cn'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"price"=>$this->input->post('price'),
								"point_gain"=>$this->input->post('point_gain'),
								"point_spend"=>$this->input->post('point_spend'),
								"img"=>$picname,
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$res = $this->db->insert('hotel_transport',$data);
						if($res) redirect('manage_product/transport_list/success');
						else  redirect('manage_product/transport_list/fail');
					}
				}
			}//end post
			
			
			$this->cismarty->assign("tiny",$this->template->tinymce());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(3,7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/transport_add"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/transport_add.html');
		}
		
		public function transport_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("journey","journey","trim");
				$this->form_validation->set_rules("distance","distance","trim");
				$this->form_validation->set_rules("title_en","name","required");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
				$this->form_validation->set_rules("desc_en","Description","required");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				$this->form_validation->set_rules("price","Price","trim|required|numeric");
				$this->form_validation->set_rules("point_gain","Point gain","trim|required|numeric");
				$this->form_validation->set_rules("point_spend","Point spend","trim|required|numeric");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
					$this->cismarty->assign("price",set_value('price'));
					$this->cismarty->assign("point_gain",set_value('point_gain'));
					$this->cismarty->assign("point_spend",set_value('point_spend'));
					$this->cismarty->assign("journey",set_value('journey'));
					$this->cismarty->assign("distance",set_value('distance'));
		
					//set error
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
					$this->cismarty->assign("errorstatus",form_error('status'));
					$this->cismarty->assign("errorprice",form_error('price'));
					$this->cismarty->assign("errorpoint_gain",form_error('point_gain'));
					$this->cismarty->assign("errorpoint_spend",form_error('point_spend'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/transport/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= '800';
 							$config['height'] = 'auto';
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$this->db->where("pkid",$id);
							$arrup = array("img"=>$picname);
							$this->db->update('hotel_transport',$arrup);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"journey"=>$this->input->post('journey'),
								"distance"=>$this->input->post('distance'),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_cn"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>addslashes(trim($this->input->post('desc_en'))),
								"description_zh_cn"=>addslashes(trim($this->input->post('desc_cn'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"price"=>$this->input->post('price'),
								"point_gain"=>$this->input->post('point_gain'),
								"point_spend"=>$this->input->post('point_spend'),
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('hotel_transport',$data);
						if($res) redirect('manage_product/transport_list/success');
						else  redirect('manage_product/transport_list/fail');
					}
				}
			}//end post
			else {
				$this->db->where("pkid",$id);
				$res = $this->db->get('hotel_transport');
				$row = $res->row();
				$this->cismarty->assign("title_en",$row->name_en);
				$this->cismarty->assign("title_cn",$row->name_zh_cn);
				$this->cismarty->assign("title_th",$row->name_th);
				$this->cismarty->assign("desc_en",stripslashes($row->description_en));
				$this->cismarty->assign("desc_cn",stripslashes($row->description_zh_cn));
				$this->cismarty->assign("desc_th",stripslashes($row->description_th));
				$this->cismarty->assign("price",$row->price);
				$this->cismarty->assign("point_gain",$row->point_gain);
				$this->cismarty->assign("point_spend",$row->point_spend);
				$this->cismarty->assign("journey",$row->journey);
				$this->cismarty->assign("distance",$row->distance);
				if($row->status == 1) $this->cismarty->assign("sa",'checked="checked"');
				else $this->cismarty->assign("sd",'checked="checked"');
				if(trim($row->img) != "") $this->cismarty->assign("img",'<img src="'.base_url().'media/transport/'.$row->img.'"><br>');
			}	
				
			$this->cismarty->assign("tiny",$this->template->tinymce());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(3,7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_product/transport_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/product/transport_edit.html');
		}
		
		public function transport_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("hotel_transport");
				if($res) redirect('manage_product/transport_list/success');
			}
		}
		
		public function transport_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("hotel_transport");
				}
			}
			redirect('manage_product/transport_list/success');
		}
		
	}
?>